package com.zhouxiaoge.controller;

import com.zhouxiaoge.entity.User;
import com.zhouxiaoge.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 周明阳
 * mail:gqzmy@outlook.com
 * 时间:2018-03-20 15:12
 */
@RestController
@RequestMapping("/api")
public class UserController {

    private final UserRepository userRepository;

    private final DiscoveryClient discoveryClient;

    @Autowired
    public UserController(UserRepository userRepository, DiscoveryClient discoveryClient) {
        this.userRepository = userRepository;
        this.discoveryClient = discoveryClient;
    }

    @GetMapping("/simple/{id}")
    public User findById(@PathVariable Long id) {
        return userRepository.getOne(id);
    }

    @GetMapping("/simple/list")
    public List<User> findAllUserInfoList() {
        return userRepository.findAll();
    }

    @GetMapping("/eureka-instance")
    public ServiceInstance serviceUrl() {
        List<ServiceInstance> instances = discoveryClient.getInstances("USER-7900");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost());
            System.out.println(instance.getPort());
            System.out.println(instance.getServiceId());
            System.out.println(instance.getMetadata());
            System.out.println(instance.getServiceId());
            System.out.println(instance.getUri());
        }
        ServiceInstance serviceInstance = instances.get(0);
        System.out.println("=================================");
        System.out.println(serviceInstance.toString());
        System.out.println("=================================");

        return serviceInstance;
    }
}
